import { Component,OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {AirFactor} from "./AirFactor";
import {HttpURLService} from "../../httpURL.service";

/**
 * 烟气过程监控因子字典管理
 * guopeng
 * 2017.11.27
 */
@Component({
  selector: 'app-airFactor',
  templateUrl: './airFactor.component.html',
  styleUrls: ['./airFactor.component.css']
})
export class AirFactorComponent implements OnInit{
  id : string;//id
  atmosphereFactorCode : string;//大气过程监测因子编码
  atmosphereFactorName : string;//大气过程监测因子名称
  atmosphereFactorUnit :string;//大气过程监测因子单位
  atmosphereFactorType : string;//大气过程监测因子类型
  extensionInfo : string;//拓展信息
  errorMessage:string;//錯誤消息

  list:Array<any>;

  queryMessage:AirFactor = new AirFactor("","","","","","");
  updateMessages:AirFactor = new AirFactor("","","","","","");
  public addMessages:AirFactor =  new AirFactor("","","","","","");

  params; // 保存页面url参数
  pageSize:any = 5;// 每页数据条数
  curPage:any = 1;// 当前页码
  totalNum: any = 0; // 总数据条数
  totalPage: any = 0;// 总页数
  url:string = this.httpURLService.getUrl("communication");
  constructor(private http: HttpClient,private httpURLService: HttpURLService){

  }
  add(){
    this.addMessages =  new AirFactor("","","","","","");
  }
  userMessage(event:AirFactor){
    let query = new AirFactor("","","","","","");
    let update = new AirFactor("","","","","","");
    Object.assign(query, event);
    Object.assign(update, event);
    this.queryMessage = query;
    this.updateMessages = update;
  }

  listMessage(info:any){
    let atmosphereFactorCode = info.atmosphereFactorCode== undefined ? '' : info.atmosphereFactorCode ;
    let atmosphereFactorName = info.atmosphereFactorName== undefined ? '' : info.atmosphereFactorName;
    this.http.get(this.url+'/airFactor/queryAirFactorListByPage?atmosphereFactorCode='+atmosphereFactorCode+'&atmosphereFactorName='+atmosphereFactorName).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  addMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/airFactor/addAirFactor',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#add").modal('show');
          this.listMessage("");
          $('#addModal').modal('hide');
        }else if(data['resultCode']=='101119101'){
          this.errorMessage = "烟气过程监控因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119102'){
          this.errorMessage = "烟气过程监控因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119103'){
          this.errorMessage = "烟气过程监控因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='1011191 04'){
          this.errorMessage = "烟气过程监控因子编码已存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }

  deleteById(event:any){
    let id = event.target.name;
    this.http.get(this.url+'/airFactor/deleteAirFactor/'+id).subscribe(data =>{
      if(data['resultCode']=='0'){
        $("#delete").modal('show');
        this.listMessage("");
        $('#deleteModal').modal('hide');
      }else if(data['resultCode']=='101119105'){
        this.errorMessage = "该烟气过程监控因子不存在！";
        $("#error").modal('show');
      }
    })
  }

  updateMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/airFactor/updateAirFactor',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#update").modal('show');
          this.listMessage("");
          $('#updateModal').modal('hide');
        }else if(data['resultCode']=='101119101'){
          this.errorMessage = "烟气过程监控因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119102'){
          this.errorMessage = "烟气过程监控因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119103'){
          this.errorMessage = "烟气过程监控因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='1011191 04'){
          this.errorMessage = "烟气过程监控因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119105'){
          this.errorMessage = "该烟气过程监控因子不存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }
  getPageData(pageNo) {
    let vm = this;
    vm.curPage = pageNo;
    let atmosphereFactorCode = this.atmosphereFactorCode== undefined ? '' : this.atmosphereFactorCode ;
    let atmosphereFactorName = this.atmosphereFactorName== undefined ? '' : this.atmosphereFactorName;
    let current = pageNo;
    this.http.get(this.url+'/airFactor/queryAirFactorListByPage?atmosphereFactorCode='+atmosphereFactorCode+'&atmosphereFactorName='+atmosphereFactorName+'&current='+current).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }
	onstructor(){ }
	ngOnInit(){
    this.http.get(this.url+'/airFactor/queryAirFactorListByPage').subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
	}
}

